<template>
  <div class="success-page">
    <div class="container">
      <button class="back-btn" @click="convertAnother">← 返回</button>

      <div class="page-header">
        <h1 class="page-title">DOCX 转 PDF 成功</h1>
      </div>

      <div class="content-card" v-if="pdfData">
        <div class="file-banner">
          <img :src="pdfPlaceholder" alt="PDF" class="banner-icon" />
          <div class="banner-text">
            <h2 class="file-name">{{ pdfData.originalFilename }}</h2>
            <p class="file-meta">
              {{ pdfData.pdfFileSizeFormatted || formatSize(pdfData.pdfFileSize) }}
              ·
              {{ formatTime(pdfData.createTime) }}
            </p>
          </div>
        </div>

        <div class="action-group">
          <button class="action-btn secondary" :disabled="downloading" @click="downloadPdf">
            <span v-if="downloading">⏳ 下载中...</span>
            <span v-else>下载 PDF</span>
          </button>
        </div>

        <div class="info-list">
          <div class="info-row">
            <span class="info-label">原始文件:</span>
            <span class="info-value">{{ pdfData.originalFilename || '-' }}</span>
          </div>
          <div class="info-row">
            <span class="info-label">原始大小:</span>
            <span class="info-value">{{ pdfData.originalFileSizeFormatted || formatSize(pdfData.originalFileSize) }}</span>
          </div>
          <div class="info-row">
            <span class="info-label">PDF 大小:</span>
            <span class="info-value">{{ pdfData.pdfFileSizeFormatted || formatSize(pdfData.pdfFileSize) }}</span>
          </div>
          <div class="info-row">
            <span class="info-label">状态:</span>
            <span class="info-value status">{{ statusText }}</span>
          </div>
        </div>
      </div>

      <div class="success-message">
        <span class="success-icon">✓</span>
        <div class="success-text">
          <strong>PDF 生成成功！</strong>
          <p>文档已经就绪，可随时预览或下载</p>
        </div>
      </div>
    </div>

  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { toastError, toastSuccess } from '@/utils/toast'
import pdfPlaceholder from '@/assets/pdf.svg'

const router = useRouter()
const pdfData = ref(null)
const downloading = ref(false)

const statusText = computed(() => {
  if (!pdfData.value) return '未知'
  if (pdfData.value.conversionStatus === 2) return '转换成功'
  if (pdfData.value.conversionStatus === 3) return '转换失败'
  return '处理中'
})

const loadData = () => {
  const storage = sessionStorage.getItem('docxToPdfResult')
  if (!storage) {
    router.push('/docx-to-pdf')
    return
  }
  try {
    pdfData.value = JSON.parse(storage)
  } catch (error) {
    router.push('/docx-to-pdf')
  }
}

onMounted(() => {
  loadData()
})

const convertAnother = () => {
  sessionStorage.removeItem('docxToPdfResult')
  router.push('/docx-to-pdf')
}

const formatSize = (size) => {
  if (!size) return '0 B'
  if (size < 1024) return `${size} B`
  if (size < 1024 * 1024) return `${(size / 1024).toFixed(2)} KB`
  return `${(size / (1024 * 1024)).toFixed(2)} MB`
}

const formatTime = (timeStr) => {
  if (!timeStr) return '-'
  const date = new Date(timeStr)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

const downloadPdf = async () => {
  if (!pdfData.value?.downloadUrl) {
    toastError('下载地址不存在')
    return
  }
  if (downloading.value) return
  downloading.value = true
  try {
    const link = document.createElement('a')
    link.href = pdfData.value.downloadUrl
    link.target = '_blank'
    link.rel = 'noopener'
    link.download = pdfData.value.pdfFilename || 'output.pdf'
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    toastSuccess('开始下载')
  } catch (error) {
    toastError('下载失败，请稍后再试')
  } finally {
    downloading.value = false
  }
}
</script>

<style scoped>
.success-page {
  min-height: 100vh;
  width: 100vw;
  background: #f5f7fa;
  padding: 40px 20px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
}

.container {
  max-width: 600px;
  width: 100%;
}

.back-btn {
  background: white;
  border: 1px solid #e5e7eb;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 0.95rem;
  color: #374151;
  cursor: pointer;
  transition: all 0.2s;
  margin-bottom: 20px;
  font-weight: 500;
}

.back-btn:hover {
  background: #f9fafb;
  border-color: #d1d5db;
}

.page-header {
  background: #e0e7ff;
  padding: 16px 24px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.page-title {
  font-size: 1.3rem;
  font-weight: 600;
  color: #1e293b;
  margin: 0;
}

.content-card {
  background: white;
  border-radius: 12px;
  padding: 28px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.file-banner {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  border-radius: 12px;
  background: #f8fafc;
  border: 1px solid #e5e7eb;
}

.banner-icon {
  width: 56px;
  height: 44px;
  object-fit: contain;
}

.file-name {
  margin: 0;
  font-size: 1.1rem;
  color: #111827;
}

.file-meta {
  margin: 4px 0 0;
  color: #6b7280;
  font-size: 0.9rem;
}

.action-group {
  display: flex;
  gap: 12px;
  margin: 24px 0;
  flex-wrap: wrap;
}

.action-btn {
  flex: 1;
  min-width: 150px;
  padding: 14px;
  border-radius: 10px;
  border: 1px solid transparent;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.action-btn.primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  box-shadow: 0 6px 16px rgba(102, 126, 234, 0.25);
}

.action-btn.primary:hover:not(:disabled) {
  transform: translateY(-2px);
}

.action-btn.secondary {
  background: white;
  border: 1px solid #d1d5db;
  color: #374151;
}

.action-btn.secondary:hover:not(:disabled) {
  background: #f9fafb;
}

.action-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.info-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.info-row {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid #f3f4f6;
}

.info-row:last-child {
  border-bottom: none;
}

.info-label {
  font-size: 0.95rem;
  color: #6b7280;
}

.info-value {
  font-weight: 600;
  color: #111827;
}

.info-value.status {
  color: #059669;
}

.success-message {
  display: flex;
  align-items: center;
  gap: 12px;
  background: #ecfdf5;
  border: 1px solid #bbf7d0;
  padding: 18px 24px;
  border-radius: 12px;
}

.success-icon {
  background: #10b981;
  color: white;
  font-size: 1.2rem;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.success-text strong {
  display: block;
  color: #065f46;
  margin-bottom: 4px;
}

.preview-modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 16px;
}

.preview-modal-content {
  background: white;
  border-radius: 16px;
  max-width: 90vw;
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  position: relative;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  overflow: hidden;
}

.preview-modal-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid #e5e7eb;
  background: #f9fafb;
}

.preview-modal-icon {
  width: 42px;
  height: 32px;
  object-fit: contain;
}

.preview-modal-title {
  flex: 1;
  font-size: 18px;
  font-weight: 600;
  color: #1f2937;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview-close-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.preview-close-btn:hover {
  background: rgba(0, 0, 0, 0.8);
  transform: rotate(90deg);
}

.preview-modal-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 12px 20px;
  font-size: 13px;
  color: #6b7280;
  border-bottom: 1px solid #f3f4f6;
}

.preview-file-container {
  flex: 1;
  padding: 30px 20px;
  background: #f9fafb;
  position: relative;
}

.preview-loading {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  justify-content: center;
  color: #6b7280;
  height: 320px;
}

.preview-loading-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid #e5e7eb;
  border-top-color: #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.preview-loading-text {
  font-size: 14px;
  color: #4b5563;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.preview-pdf-frame {
  width: 100%;
  height: 520px;
  border: none;
  border-radius: 12px;
  background: white;
  box-shadow: inset 0 0 0 1px rgba(15, 23, 42, 0.08);
}

.preview-modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 20px;
  border-top: 1px solid #e5e7eb;
  background: #f9fafb;
}

.preview-modal-btn {
  min-width: 120px;
  padding: 10px 18px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.preview-modal-btn.primary {
  background: #ecfeff;
  border-color: #a5f3fc;
  color: #0e7490;
}

.preview-modal-btn.primary:hover:not(:disabled) {
  background: #cffafe;
  border-color: #67e8f9;
}

.preview-modal-btn.secondary {
  background: white;
  border-color: #e5e7eb;
  color: #6b7280;
}

.preview-modal-btn.secondary:hover {
  background: #f3f4f6;
}

.preview-modal-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

@media (max-width: 768px) {
  .content-card {
    padding: 20px;
  }

  .file-banner {
    flex-direction: column;
    align-items: flex-start;
  }

  .action-group {
    flex-direction: column;
  }

  .preview-modal-content {
    max-width: 95vw;
    max-height: 95vh;
  }

  .preview-file-container {
    padding: 20px 12px;
  }
}
</style>

